/**
 * 便捷方法
 * @author ace
 * @create 2017/5/9
 */
class GameHandy {

    //映射方向
    static mappingDir(pos) {
        if (!pos) return;

        PublicVal.i.dir2Pos = {};
        PublicVal.i.pos2Dir = {};

        let a = pos;
        let b = 1 + (PublicVal.i.ownPos) % game.roomSeat;
        let c = 1 + (PublicVal.i.ownPos + 1) % game.roomSeat;
        let d = 1 + (PublicVal.i.ownPos + 2) % game.roomSeat;

        PublicVal.i.dir2Pos[1] = a;
        PublicVal.i.pos2Dir[a] = 1;

        switch (game.roomSeat) {
            case 2:
                PublicVal.i.dir2Pos[3] = b;
                PublicVal.i.pos2Dir[b] = 3;
                break;
            case 3:
                PublicVal.i.dir2Pos[2] = b;
                PublicVal.i.pos2Dir[b] = 2;
                PublicVal.i.dir2Pos[4] = c;
                PublicVal.i.pos2Dir[c] = 4;
                break;
            case 4:
                PublicVal.i.dir2Pos[2] = b;
                PublicVal.i.pos2Dir[b] = 2;
                PublicVal.i.dir2Pos[3] = c;
                PublicVal.i.pos2Dir[c] = 3;
                PublicVal.i.dir2Pos[4] = d;
                PublicVal.i.pos2Dir[d] = 4;
                break;
        }

    }

    //打牌被禁止
    static shotBanned(value: boolean) {
        let handCon = GSController.i.gsView.MJViews[1].handCon;
        let card;
        for (let i: number = 0; i < handCon.numChildren; i++) {
            card = <CardView>handCon.getChildAt(i);
            if (card && card.index > -1) {
                card.touchEnabled = card.enabled = !value;
            }
        }
    }

    //打牌被指定
    static shotDesignated(pais: {type: number, number: number}[] = []) {

        if (!pais)return;

        if (PublicVal.state == StateType.ting)return;

        if (pais.length) {
            this.shotBanned(true);

            let handCon = GSController.i.gsView.MJViews[1].handCon;
            let card;
            let pai;
            for (let i: number = 0; i < handCon.numChildren; i++) {
                card = <CardView>handCon.getChildAt(i);
                if (card && card.index > -1) {
                    for (let j: number = 0; j < pais.length; j++) {
                        pai = pais[j];
                        if (pai.number == card.pai.number && pai.type == card.pai.type) {
                            card.touchEnabled = card.enabled = true;
                            // card.y = card.pos.y - 25;
                            pais.splice(j, 1);
                            break;
                        }
                    }
                }
            }
        }
    }

    //暗杠显示
    static turnAngang(pos: number, isUpdate: boolean = false) {
        let dir = PublicVal.i.getDir(pos);
        let funcPais = PublicVal.i.getFuncPais(dir);

        let obj:any;
        for (let i: number = 0; i < funcPais.length; i++) {
            obj = funcPais[i];
            if (obj.action == 24) {
                // obj.action = 25;
                obj.unan = 1;
            }
        }
        isUpdate && GSController.i.updateMJView(dir);
    }

    //自动打出花牌
    static shotHua() {
        let hand = GameHandy.getCards();
        let huas = GameHandy.getCards(CardType.hua);
        if (GSConfig.handLens[hand.length] && huas.length) {
            //花牌需要自动打出去
            GSController.i.sendPai(huas[0], 500);
        }
    }

    //检查花牌
    static checkHua() {
        let handPais: any[] = PublicVal.i.getHandPais(1);
        if (handPais && handPais.length) {
            let pai: any;
            let pais: any[] = [];
            for (let i: number = 0; i < handPais.length; i++) {
                pai = handPais[i];
                if (pai && pai.type == CardType.hua) {
                    pais.push(pai);
                }
            }

            if (pais.length) {
                this.shotDesignated(pais);
            }
        }
    }

    //检查明搂
    static checkMinglou(dir: number, minglou: any[]) {
        if (minglou && minglou.length) {
            let mjview: GameSceneMJLayer = GSController.i.gsView.MJViews[dir];
            let head: HeadIcon = GSController.i.gsView.getHeadView(dir);
            for (let i: number = 0; i < minglou.length; i++) {
                mjview.pushHu(minglou[i]);
                head.isMinglou = true;
            }
        }
    }

    //获取最短的牌类型
    static getCardsShortest(limitLength: number = -1): CardType {
        let list: any[][] = [
            [CardType.wan, this.getCardsLength(CardType.wan)],
            [CardType.tiao, this.getCardsLength(CardType.tiao)],
            [CardType.tong, this.getCardsLength(CardType.tong)]
        ];

        list.sort(function (a, b) {
            if (a[1] < b[1]) {
                return -1;
            }
            else if (a[1] > b[1]) {
                return 1;
            }
            else {
                return 0;
            }
        });

        if (limitLength == -1) {
            return list[0][0];
        }
        else {
            for (let i: number = 0; i < list.length; i++) {
                length = list[i][1];
                if (list[i][1] >= limitLength) {
                    return list[i][0];
                }
            }
        }
    }

    //获取某个类型牌的长度
    static getCardsLength(type: CardType): number {
        let handCards: any[] = PublicVal.i.getHandPais(1);
        let length: number = 0;
        let card: any;
        if (handCards) {
            for (let i: number = 0; i < handCards.length; i++) {
                card = handCards[i];
                if (card && card.type == type) {
                    length++;
                }
            }
        }
        return length;
    }

    //获取某个类型牌的队列
    static getCards(type: CardType = -1, length: number = -1): any[] {
        let handCards: any[] = PublicVal.i.getHandPais(1);
        let cards: any[] = [];
        let card: any;

        if (handCards) {
            for (let i: number = 0; i < handCards.length; i++) {
                card = handCards[i];
                if (card) {
                    if (type == -1 || card.type == type) {
                        cards.push(card);
                    }
                }

                if (length != -1 && cards.length == length) {
                    break;
                }
            }
        }
        return cards;
    }
}